package com.tsingteng.cosfun.ui.message.addplay;

import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.base.BasePresenterFragment;
import com.tsingteng.cosfun.bean.DBBean;
import com.tsingteng.cosfun.bean.DBPlayBean;
import com.tsingteng.cosfun.utils.KeyBoardUtils;
import com.tsingteng.cosfun.widget.MyRecycleView;
import com.tsingteng.cosfun.widget.TitleView;
import com.tsingteng.cosfun.widget.dialog.ContentDialog;
import com.tsingteng.cosfun.widget.dialog.WarningDialog;

import java.io.Serializable;

import butterknife.BindView;
import butterknife.OnClick;

/***
 * 写便当
 * 常胜；
 * 2018.8.11
 * */
public class WritingPlayFragment extends BasePresenterFragment<WritingPresenter, WritingPlayContract.IWritingView> implements WritingPlayContract.IWritingView ,TextWatcher{
    @BindView(R.id.tv_writing_title)
    TitleView tvWritingTitle;
    @BindView(R.id.ll_drafts)
    LinearLayout llDrafts;
    @BindView(R.id.ll_publish)
    LinearLayout llPublish;
    @BindView(R.id.et_comment_play)
    EditText mEtComment;
    private DBPlayBean dbBean;

    public static WritingPlayFragment newInstance(DBPlayBean dbBean) {
        WritingPlayFragment fragment = new WritingPlayFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("dbBean",dbBean);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void getBundle(Bundle bundle) {
        super.getBundle(bundle);
        dbBean = (DBPlayBean) bundle.getSerializable("dbBean");
    }

    @Override
    protected void initData(View rootView) {
        mEtComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                KeyBoardUtils.searchPoint(mEtComment);
            }
        });
//        //通过过滤器设置最大输入字数是500
//        mEtComment.setFilters(new InputFilter[]{new InputFilter.LengthFilter(500)});
        if (dbBean!= null) {
            mEtComment.setText(dbBean.getPath());
            mEtComment.setSelection(mEtComment.getText().toString().length());
        }
        tvWritingTitle.setTitle("做饭");
        tvWritingTitle.setIvLeft(R.drawable.close_icon);
        tvWritingTitle.setOnTitleClickListener(new TitleView.OnTitleClickListener() {
            @Override
            public void onLeft() {
                if (TextUtils.isEmpty(mEtComment.getText().toString().trim())) {
                    getActivity().finish();
                    return;
                }
                new ContentDialog(getContext()).setContent("确定要放弃发便当吗?").setAffirmContent("确定").setAffirmContentColor(R.color.red_bag_color).setCancelContent("取消").setOnTwoClickListener(new ContentDialog.OnTwoClickListener() {
                    @Override
                    public void onAffirm() {
                        getActivity().finish();
                    }

                    @Override
                    public void onCancel() {

                    }
                }).show();
            }

            @Override
            public void onRight() {

            }

            @Override
            public void onRight2() {

            }
        });

        mEtComment.addTextChangedListener(this);
    }

    @Override
    protected WritingPresenter createPresenter() {
        return new WritingPresenter();
    }

    @Override
    protected View initHeader() {
        return null;
    }

    @Override
    protected MyRecycleView createRecycleView() {
        return null;
    }

    @Override
    protected BaseQuickAdapter createAdapter() {
        return null;
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

    }

    @Override
    public void onLoadMoreRequested() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_writing_play;
    }


    @Override
    protected void refresh() {

    }


    @OnClick({R.id.ll_drafts, R.id.ll_publish})
    public void onViewClicked(View view) {
        String comment = mEtComment.getText().toString();
        if (TextUtils.isEmpty(comment)) {
            showToast("内容不能为空!");
            return;
        }
        switch (view.getId()) {
            case R.id.ll_drafts:
                DBPlayBean dbBean = new DBPlayBean();
                dbBean.setName("play");
                dbBean.setTime(System.currentTimeMillis());
                dbBean.setPath(comment);
                if (dbBean.save()) {
                    showToast("保存成功!");
                    getActivity().finish();
                }else {
                    showToast("保存失败!");
                }
                break;
            case R.id.ll_publish:
                mPresenter.pulishPlay(comment);
                break;
        }
    }

    @Override
    public void showResult(String status) {
        if (TextUtils.equals(status,"1")) {
            showToast("发布成功!");
            getActivity().finish();
        } else {
            showToast("发布失败!");
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (mEtComment.length() >= 500) {
//            WarningDialog warningDialog = new WarningDialog(getContext());
//            warningDialog.show();
//            new Handler().postDelayed(new Runnable() {
//                @Override
//                public void run() {
//                    warningDialog.dissMiss();
//                }
//            },1000);
            Toast.makeText(getActivity(),getResources().getString(R.string.limit_msg),Toast.LENGTH_LONG).show();
        }
    }
}
